package com.tianbing.adlottery.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.fengniu.ejb.business.ISiteBusiness;
import com.fengniu.ejb.business.IUserBusiness;
import com.fengniu.ejb.dao.ISiteDao;
import com.fengniu.ejb.dao.IUserDao;
import com.fengniu.ejb.entity.Category;
import com.fengniu.ejb.entity.DeltaRelation;
import com.fengniu.ejb.entity.Site;
import com.fengniu.ejb.entity.Site2Site;
import com.fengniu.ejb.entity.User2Content;
import com.fengniu.ejb.entity.User2Site;
import com.tianbing.adlottery.global.IConstants;
import com.tianbing.adlottery.helper.SkinUtils;
import com.tianbing.adlottery.util.ControllerFormBean;
import com.tianbing.adlottery.util.UserSession;
import com.tianbing.adlottery.util.UtilDataGroup;

/**
 * MultiActionController for the image list/upload UI.
 * 
 * @author Conan Chen
 * @since 2006.12.15
 */
public class HordeController extends MultiActionController {
	Logger logger = Logger.getLogger(HordeController.class.getName());

	private String hordeDetailView;

	private String ajaxGetHordeDetailView;

	private String listHordeFloatBookView;

	private String listHordeRecommendStoreCouponView;

	private String listHordeRecommendStoreView;

	private String listHordeRecommendStoreProductView;

	private String floatBookDetailView;

	private ISiteDao siteDao;

	private IUserDao userDao;

	private SkinUtils skinUtils;

	private IUserBusiness userBusiness;

	private ISiteBusiness siteBusiness;

	@SuppressWarnings("unchecked")
	public ModelAndView ajaxGetHordeDetail(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Category siteCategory = null;
		List<Category> primarySiteCategoryTrail = null;

		ModelAndView mv = new ModelAndView(ajaxGetHordeDetailView);
		Map model = mv.getModel();

		// get site store detail
		Long ssid = ServletRequestUtils.getLongParameter(request, "ssid", -1);
		Site siteDetail = siteDao.findSiteBySysId(ssid);

		if (siteDetail != null) {
			// get site's cateogry trail
			primarySiteCategoryTrail = siteBusiness.getSiteLongestCategoryTrail(ssid, IConstants.CategoryID.AREA_TOP
					.getId());
			if (primarySiteCategoryTrail != null && primarySiteCategoryTrail.size() > 0) {
				siteCategory = primarySiteCategoryTrail.get(primarySiteCategoryTrail.size() - 1);
			}
			// get post2 site message
			// List<Site2Content> site2ContentList =
			// siteBusiness.getListSitePostMessage(ssid, 0, 30);
			// get site session

			List<Site2Site> pageSections = siteBusiness.getSitePageSections(ssid);
			model.put("topSection", getPageSection(pageSections, "_TOP"));
			model.put("leftSection", getPageSection(pageSections, "_LEFT"));

			// get horde recommendded stores
			Map hordeRecommendStores = siteBusiness.getHordeRecommendStores(ssid, -1, 0, 8);

			List site2SiteList = (List) hordeRecommendStores.get("site2SiteList");
			Long totalCount = (Long) hordeRecommendStores.get("totalCount");
			model.put("hordeRecommendedStoreList", site2SiteList);

			// get hode recommendded stores' product
			Map hordeRecommendStoreProducts = siteBusiness.getHordeRecommendStoreProducts(ssid, -1, 0, 8);
			List<DeltaRelation> hordeRecommendStoreProductList = (List<DeltaRelation>) hordeRecommendStoreProducts
					.get("site2StoreProductList");
			List hordeRecommendStoreProductGroupList = UtilDataGroup.makeGroups(hordeRecommendStoreProductList, 0, 10);
			model.put("hordeRecommendStoreProductGroupList", hordeRecommendStoreProductGroupList);

			// get hode recommendded stores' coupon
			Map hordeRecommendStoreCoupons = siteBusiness.getHordeRecommendStoreCoupons(ssid, -1, 0, 10);
			List<DeltaRelation> hordeRecommendedStoreCouponList = (List<DeltaRelation>) hordeRecommendStoreCoupons
					.get("site2StoreCouponList");
			List hordeRecommendStoreCouponGroupList = UtilDataGroup.makeGroups(hordeRecommendedStoreCouponList, 0, 10);
			model.put("hordeRecommendStoreCouponGroupList", hordeRecommendStoreCouponGroupList);

			// get horde servant
			User2Site user2ManagedHorde = userBusiness.findUserManagedHorde(-1, ssid);
			model.put("user2ManagedHorde", user2ManagedHorde);

			// model.put("site2ContentList", site2ContentList);
			model.put("siteCategory", siteCategory);
			model.put("primarySiteCategoryTrail", primarySiteCategoryTrail);
			model.put("siteDetail", siteDetail);
		}
		return mv;
	}

	@SuppressWarnings("unchecked")
	public ModelAndView viewHordeDetail(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(hordeDetailView);

		Map model = mv.getModel();

		// get site store detail
		Long ssid = ServletRequestUtils.getLongParameter(request, "ssid", -1);
		model.put("ssid", ssid);

		// get the login user's favourite websites
		model.put("myFavouriteWebSiteSize", 0);
		UserSession auth = skinUtils.getUserAuthorization(request, response);
		if (auth != null && !auth.isLogout()) {
			List<User2Site> u2ss = userBusiness.getListUserFavoriteWebSite(auth.getUserSysId(), -1, 0, 30);
			model.put("myFavouriteWebSiteList", u2ss);
			model.put("myFavouriteWebSiteSize", u2ss == null ? 0 : u2ss.size());
		}

		// set lasted visited horde
		skinUtils.setLastVisitedHorde(request, response);
		return mv;
	}

	@SuppressWarnings("unchecked")
	public ModelAndView listHordeRecommendStore(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Category siteCategory = null;
		List primarySiteCategoryTrail = null;
		ModelAndView mv = new ModelAndView(listHordeRecommendStoreView);
		Map model = mv.getModel();

		Integer pgno = ServletRequestUtils.getIntParameter(request, "pgno", 0);
		Integer pgsize = 30;// ServletRequestUtils.getIntParameter(request,
		// "pgsize", 30);

		// get horde site detail
		Long ssid = ServletRequestUtils.getLongParameter(request, "ssid", -1);
		Site hordeDetail = siteDao.findSiteBySysId(ssid);
		model.put("hordeDetail", hordeDetail);

		// get hode recommendded stores' coupon
		Map hordeRecommendStores = siteBusiness.getHordeRecommendStores(ssid, -1, pgno, pgsize);
		List<DeltaRelation> hordeRecommendStoreList = (List<DeltaRelation>) hordeRecommendStores.get("site2SiteList");
		model.put("hordeRecommendStoreList", hordeRecommendStoreList);
		Long totalCount = (Long) hordeRecommendStores.get("totalCount");

		return mv;
	}

	@SuppressWarnings("unchecked")
	public ModelAndView listHordeRecommendStoreCoupon(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Category siteCategory = null;
		List primarySiteCategoryTrail = null;
		ModelAndView mv = new ModelAndView(listHordeRecommendStoreCouponView);
		Map model = mv.getModel();

		Integer pgno = ServletRequestUtils.getIntParameter(request, "pgno", 0);
		Integer pgsize = 30;// ServletRequestUtils.getIntParameter(request,
		// "pgsize", 30);

		// get site store detail
		Long ssid = ServletRequestUtils.getLongParameter(request, "ssid", -1);
		Site hordeDetail = siteDao.findSiteBySysId(ssid);

		// get hode recommendded stores' coupon
		Map hordeRecommendStoreCoupons = siteBusiness.getHordeRecommendStoreCoupons(ssid, -1, pgno, pgsize);
		List<DeltaRelation> hordeRecommendStoreProductList = (List<DeltaRelation>) hordeRecommendStoreCoupons
				.get("site2StoreCouponList");
		model.put("hordeRecommendStoreCouponList", hordeRecommendStoreProductList);
		Long totalCount = (Long) hordeRecommendStoreCoupons.get("totalCount");

		model.put("hordeDetail", hordeDetail);

		return mv;
	}

	@SuppressWarnings("unchecked")
	public ModelAndView listHordeRecommendStoreProduct(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Category siteCategory = null;
		List primarySiteCategoryTrail = null;
		ModelAndView mv = new ModelAndView(listHordeRecommendStoreProductView);
		Map model = mv.getModel();

		Integer pgno = ServletRequestUtils.getIntParameter(request, "pgno", 0);
		Integer pgsize = 30;// ServletRequestUtils.getIntParameter(request,
		// "pgsize", 30);

		// get horde detail
		Long ssid = ServletRequestUtils.getLongParameter(request, "ssid", -1);
		Site hordeDetail = siteDao.findSiteBySysId(ssid);

		// get hode recommendded stores' product
		Map hordeRecommendStoreCoupons = siteBusiness.getHordeRecommendStoreProducts(ssid, -1, pgno, pgsize);
		List<DeltaRelation> hordeRecommendStoreProductList = (List<DeltaRelation>) hordeRecommendStoreCoupons
				.get("site2StoreProductList");
		model.put("hordeRecommendStoreProductList", hordeRecommendStoreProductList);
		Long totalCount = (Long) hordeRecommendStoreCoupons.get("totalCount");

		model.put("hordeDetail", hordeDetail);

		return mv;
	}

	@SuppressWarnings("unchecked")
	public ModelAndView listHordeFloatBook(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Category siteCategory = null;
		List primarySiteCategoryTrail = null;
		ModelAndView mv = new ModelAndView(listHordeFloatBookView);
		Map model = mv.getModel();

		Integer pgno = ServletRequestUtils.getIntParameter(request, "pgno", 0);
		Integer pgsize = 30;// ServletRequestUtils.getIntParameter(request,
		// "pgsize", 30);

		// get site store detail
		Long ssid = ServletRequestUtils.getLongParameter(request, "ssid", -1);
		Site siteDetail = siteDao.findSiteBySysId(ssid);
		List<DeltaRelation> site2User_ContentList = userBusiness.getListSiteUserFloatBook(ssid, pgno, pgsize);
		model.put("site2User_ContentList", site2User_ContentList);

		return mv;
	}

	@SuppressWarnings("unchecked")
	public ModelAndView viewFloatBookDetail(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Category siteCategory = null;
		List primarySiteCategoryTrail = null;
		ModelAndView mv = new ModelAndView(floatBookDetailView);
		Map model = mv.getModel();

		Integer pgno = ServletRequestUtils.getIntParameter(request, "pgno", 0);
		Integer pgsize = 30;// ServletRequestUtils.getIntParameter(request,
		// "pgsize", 30);

		Long u2csid = ServletRequestUtils.getLongParameter(request, "u2csid", -1);
		User2Content floatUser2Book = userDao.findUser2ContentBy(u2csid);
		model.put("floatUser2Book", floatUser2Book);
		if (floatUser2Book != null) {
			// TODO get reply messages for the float book-- ��ø���
			List<DeltaRelation> deltaList = userBusiness.getlistUserFloatBookRepliedMessage(u2csid, pgno, pgsize);
			model.put("floatUser2User_ContentList", deltaList);

			UserSession auth = skinUtils.getUserAuthorization(request, response);
			if (auth != null && !auth.isLogout()) {
				DeltaRelation deltaRelation = new DeltaRelation();
				deltaRelation.setUser2Content2(floatUser2Book);
				ControllerFormBean fb = new ControllerFormBean();
				fb.setDeltaUCSGRelation(deltaRelation);
				model.put("command", fb);
			}
		}
		return mv;
	}

	private Site2Site getPageSection(List<Site2Site> pageSections, String sectionName) {
		if (pageSections != null && pageSections.size() > 0) {
			for (int i = 0; i < pageSections.size(); i++) {
				Site2Site s2s = pageSections.get(i);
				if (s2s.getName().equals(sectionName)) {
					return s2s;
				}
			}
		}
		return null;
	}

	public void setLogger(Logger logger) {
		this.logger = logger;
	}

	public void setHordeDetailView(String hordeDetailView) {
		this.hordeDetailView = hordeDetailView;
	}

	public void setAjaxGetHordeDetailView(String ajaxGetHordeDetailView) {
		this.ajaxGetHordeDetailView = ajaxGetHordeDetailView;
	}

	public void setListHordeFloatBookView(String listHordeFloatBookView) {
		this.listHordeFloatBookView = listHordeFloatBookView;
	}

	public void setListHordeRecommendStoreCouponView(String listHordeRecommendStoreCouponView) {
		this.listHordeRecommendStoreCouponView = listHordeRecommendStoreCouponView;
	}

	public void setListHordeRecommendStoreProductView(String listHordeRecommendStoreProductView) {
		this.listHordeRecommendStoreProductView = listHordeRecommendStoreProductView;
	}

	public void setFloatBookDetailView(String floatBookDetailView) {
		this.floatBookDetailView = floatBookDetailView;
	}

	public void setSiteDao(ISiteDao siteDao) {
		this.siteDao = siteDao;
	}

	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}

	public void setSkinUtils(SkinUtils skinUtils) {
		this.skinUtils = skinUtils;
	}

	public void setUserBusiness(IUserBusiness userBusiness) {
		this.userBusiness = userBusiness;
	}

	public void setSiteBusiness(ISiteBusiness siteBusiness) {
		this.siteBusiness = siteBusiness;
	}

	public void setListHordeRecommendStoreView(String listHordeRecommendStoreView) {
		this.listHordeRecommendStoreView = listHordeRecommendStoreView;
	}

}
